/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2013-9-3         Rico
 */

package com.tianci.appstore.utils;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class SkyFixedThreadPool {
    static int MAX_THREAD_NUM = 10;
    static int FIXED_THREAD_NUM = 3;

    ExecutorService mCallService;

    Thread mThread;

    int mTimeoutSec = 10;

    boolean cancel = false;

    SkyTaskRunnablePool taskPool;

    static String mSession;

    public SkyFixedThreadPool(int threadnumber, boolean limitTask) {
        if (threadnumber > 0) {
            FIXED_THREAD_NUM = threadnumber;
        }
        mTimeoutSec = 5;

        mThread = new Thread(new ProcessTaskRunnable());
        mThread.setName("picture_thread");

        taskPool = new SkyTaskRunnablePool(limitTask);

        mCallService = Executors.newFixedThreadPool(FIXED_THREAD_NUM);

        startAll();
    }

    public void startAll() {
        mThread.start();
    }

    public void stopAll() {
        cancel = true;
        taskPool.cleanup();
    }

    public void execute(SkyRunnable task) {
        taskPool.putTask(task);
    }

    public static void setSession(String session) {
        mSession = session;
    }

    public class ProcessTaskRunnable implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true && !cancel) {
                SkyRunnable calltask = null;
                calltask = taskPool.getTask();
                if (calltask != null) {
                    Future<String> future = mCallService.submit(calltask);
                    try {
                        String result = future.get(mTimeoutSec, TimeUnit.SECONDS);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        System.out.println("rest call " + calltask.getRunnableId() + " throw interrupt exception");
//                      e.printStackTrace();
                    } catch (ExecutionException e) {
                        // TODO Auto-generated catch block
                        System.out.println("rest call " + calltask.getRunnableId() + " throw execute exception");
//                      e.printStackTrace();
                    } catch (TimeoutException e) {
                        // TODO Auto-generated catch block
                        System.out.println("rest call " + calltask.getRunnableId() + " throw timeout exception");
//                      e.printStackTrace();
                    }
                } else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            mCallService.shutdown();


        }

    }
}
